package com.yft.util;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;

public class NumUtils {
	public static double ROUND_HALF_UP(double price,int num){
		return new BigDecimal(price).setScale(num,BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	public static float ROUND_HALF_UP(float price,int num){
		return new BigDecimal(price).setScale(num,BigDecimal.ROUND_HALF_UP).floatValue();
	}
	public static Double getDoubleMedian(List<Double> numbers){
		//new BigDecimal(numbers.size()).setScale(0,BigDecimal.ROUND_CEILING).intValue();
		int m = numbers.size();
		if (m==1) return numbers.get(0);
		else if (m%2!=0){
			return numbers.get(m/2+1);
		}else{
			return (numbers.get(m/2)+numbers.get(m/2+1))/2;
		}
	}

	public static Float getFloatMedian(List<Float> numbers){
		//new BigDecimal(numbers.size()).setScale(0,BigDecimal.ROUND_CEILING).intValue();
		int m = numbers.size();
		if (m==1) return numbers.get(0);
		else if (m%2!=0){
			return numbers.get(m/2+1);
		}else{
			return (numbers.get(m/2)+numbers.get(m/2+1))/2;
		}
	}

	 public static Float getStandardDiviation(float sqrsum,int count,float average) {
		 float result;

	  result = (float)Math.sqrt(Math.abs((sqrsum - count * average * average) / count));
	  
	  return result;
	 }
	 
	 
 public static float getStandard(float score,float average,float standiff) {  //��׼��
		 if (standiff==0) return 0;
		 return (score-average) /standiff;

	 }

 public static double getMax(double[] inputData) {
	  if (inputData == null || inputData.length == 0)
	   return -1;
	  int len = inputData.length;
	  double max = inputData[0];
	  for (int i = 0; i < len; i++) {
	   if (max < inputData[i])
	    max = inputData[i];
	  }
	  return max;
	 }

	
	 public static double getMin(double[] inputData) {
	  if (inputData == null || inputData.length == 0)
	   return -1;
	  int len = inputData.length;
	  double min = inputData[0];
	  for (int i = 0; i < len; i++) {
	   if (min > inputData[i])
	    min = inputData[i];
	  }
	  return min;
	 }


	 public static double getSum(double[] inputData) {
	  if (inputData == null || inputData.length == 0)
	   return -1;
	  int len = inputData.length;
	  double sum = 0;
	  for (int i = 0; i < len; i++) {
	   sum = sum + inputData[i];
	  }

	  return sum;

	 }


	 public static int getCount(double[] inputData) {
	  if (inputData == null)
	   return -1;

	  return inputData.length;
	 }


	 public static double getAverage(double[] inputData) {
	  if (inputData == null || inputData.length == 0)
	   return -1;
	  int len = inputData.length;
	  double result;
	  result = getSum(inputData) / len;
	  
	  return result;
	 }

	 public static double getSquareSum(double[] inputData) {
	  if(inputData==null||inputData.length==0)
	      return -1;
	     int len=inputData.length;
	  double sqrsum = 0.0;
	  for (int i = 0; i <len; i++) {
	   sqrsum = sqrsum + inputData[i] * inputData[i];
	  }

	  
	  return sqrsum;
	 }


	 public static double getVariance(double[] inputData) {
	  int count = getCount(inputData);
	  double sqrsum = getSquareSum(inputData);
	  double average = getAverage(inputData);
	  double result;
	  result = (sqrsum - count * average * average) / count;

	     return result; 
	 }


	 public static double getStandardDiviation(double[] inputData) {
	  double result;
	  result = Math.sqrt(Math.abs(getVariance(inputData)));
	  
	  return result;

	 }
	 
	 
	 public static double[] String2double(String[] inputData){
		 double[] result = new double[inputData.length];
		 
		 int i=0;
		 for(String n:inputData){
			 result[i] = NumberUtils.toDouble(n);
			 i++;
		 }
		 
		 return result;
	 }


	
}
